package com.baizhi.commons;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class ShiroTest1 {
    private String userName="Chy";
    private String password="sb";


    @Test
    public void shiro(){
        //1.读取配置文件 IniSecurityManagerFactory 读取配置文件  封装到安全管理器中
        IniSecurityManagerFactory managerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");        SecurityManager securityManager = managerFactory.getInstance();

        //2.将安全管理器给SecurityUtils工具类
        SecurityUtils.setSecurityManager(securityManager);

        //3.在任何位置获取Subject通过SecuritUtils工具类获取
        Subject subject = SecurityUtils.getSubject();

        //4.把用户输入的账号和认证信息封装到Token中
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        //5.调用登录方法 参数是Token  Token 令牌 封装认证相关信息

        try{
            subject.login(token);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
        }catch (UnknownAccountException e1){
            System.out.println("账号错误");
        }
        boolean authenticated = subject.isAuthenticated();
        System.out.println(authenticated);

        /* subject.login(token);*/


    }
}
